'use strict'
const path = require('path')
function resolve(dir) {
  return path.join(__dirname, dir)
}
const name = process.env.VUE_APP_TITLE || '小诗科技' // 网页标题
const CompressionPlugin = require('compression-webpack-plugin');
const baseApi = "http://127.0.0.1:9527";
const port = process.env.port || process.env.npm_config_port || 9090 // dev port
const webpack = require('webpack');
module.exports = {
  // 部署生产环境和开发环境下的URL。
  // 默认情况下，Vue CLI 会假设你的应用是被部署在一个域名的根路径上
  // 例如 https://www.xshi.com/。如果应用被部署在一个子路径上，你就需要用这个选项指定这个子路径。例如，如果你的应用被部署在 https://www.xshi.com/admin/，则设置 baseUrl 为 /admin/。
  publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
  // 在npm run build 或 yarn build 时 ，生成文件的目录名称（要和baseUrl的生产环境路径一致）（默认dist）
  outputDir: 'dist',
  // 用于放置生成的静态资源 (js、css、img、fonts) 的；（项目打包之后，静态资源会放在这个文件夹下）
  assetsDir: 'static',
  // 是否开启eslint保存检测，有效值：ture | false | 'error'
  lintOnSave: process.env.NODE_ENV === 'development',
  // 如果你不需要生产环境的 source map，可以将其设置为 false 以加速生产环境构建。
  productionSourceMap: false,
  configureWebpack: {
    plugins: [      
      new webpack.ProvidePlugin({
        'window.Quill': 'quill/dist/quill.js',
        'Quill': 'quill/dist/quill.js'
      })
    ],
    resolve: {
      alias: {
        // If using the runtime only build
        vue$: "vue/dist/vue.runtime.esm.js" // 'vue/dist/vue.runtime.common.js' for webpack 1
        // Or if using full build of Vue (runtime + compiler)
        // vue$: 'vue/dist/vue.esm.js'      // 'vue/dist/vue.common.js' for webpack 1
      }
    }
  },
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    // before: require('./mock/mock-server.js'),
    proxy: {
      '/sysApi': { //平台中心API
        target: baseApi+'/sys', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/sysApi': '/'
        }
      },
      '/oauthApi': { //授权中心API
        target: baseApi+'/oauth', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/oauthApi': '/'
        }
      },
      '/ompApi':{ //运管平台API
        target: baseApi+'/omp', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/ompApi': '/'
        }
      },
      '/fileApi':{ //文件中心API
        target: baseApi+'/file', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/fileApi': '/'
        },
      },
      '/logApi':{ //日志中心API
        target: baseApi+'/log', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/logApi': '/'
        }
      },
      '/jobApi':{ //调度中心API
        target: baseApi+'/job', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/jobApi': '/'
        },
      },
      '/bpmApi':{ //工作流中心API
        target: baseApi+'/bpm', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/bpmApi': '/'
        },
      },
      '/imApi':{ //即时通讯中心API
        target: baseApi+'/im', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/imApi': '/'
        },
      },
      '/imWebSocketApi':{  //即时通讯中心WebSocket
        target: baseApi+'/imWebSocket', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/imWebSocketApi': '/'
        },
      },
      '/monitorApi':{ //监控中心API
        target: baseApi+'/iotMonitor', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/monitorApi': '/'
        }
      },
      '/opApi':{ //运维中心API
        target: baseApi+'/iotOp', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/opApi': '/'
        },
      },
      '/reportApi':{
        target: baseApi+'/report', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/reportApi': '/'
        },
      },
      '/medicalApi':{
        target: baseApi+'/medical', // http://localhost:7001  http://171.16.55.16:32181
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/medicalApi': '/'
        },
      }
    }
  },
  chainWebpack: config => {
    config.plugins.delete('preload') // TODO: need test
    config.plugins.delete('prefetch') // TODO: need test

    if(process.env.NODE_ENV !== 'development'){
      // 对超过10kb的文件gzip压缩
      config.plugin('compressionPlugin').use(
        new CompressionPlugin({
          cache: false,                                  // 不启用文件缓存
          test: /\.(js|css|html|jpeg|png|gif|svg)?$/i,   // 压缩文件格式
          filename: '[path][base].gz[query]',            // 压缩后的文件名
          algorithm: 'gzip',                             // 使用gzip压缩
          minRatio: 0.8,                                 // 压缩比例，小于 80% 的文件不会被压缩
          //exclude: /(node_modules|bower_components)/,    //排除，不用转化的
          exclude: /ohjs|deng/,                          // 排除OHJS和deng文件夹中的文件
          deleteOriginalAssets: false                    // 压缩后删除原文件
        })
      );

      // set svg-sprite-loader
      config.module
        .rule('svg')
        .exclude.add(resolve('src/assets/icons'))
        .end()
      config.module
        .rule('icons')
        .test(/\.svg$/)
        .include.add(resolve('src/assets/icons'))
        .end()
        .use('svg-sprite-loader')
        .loader('svg-sprite-loader')
        .options({
          symbolId: 'icon-[name]'
        })
        .end()
    }

    config.when(process.env.NODE_ENV !== 'development', config => {
      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [{
          // `runtime` must same as runtimeChunk name. default is `runtime`
          inline: /runtime\..*\.js$/
        }])
        .end()

      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial' // only package third parties that are initially dependent
          },
          elementUI: {
            name: 'chunk-elementUI', // split elementUI into a single package
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
            priority: 20 // the weight needs to be larger than libs and app or it will be packaged into libs or app
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'), // can customize your rules
            minChunks: 3, //  minimum common number
            priority: 5,
            reuseExistingChunk: true
          }
        }
      })
      config.optimization.runtimeChunk('single')
    })
  },
  css: {
    loaderOptions: {
      postcss: {
        config: {
          path: __dirname
        }
      },
      scss: {
        prependData: `@import "@/assets/sass/vendors/vue/vuetify/variables.scss";`
      }
    }
  },
  transpileDependencies: ["vuetify"]
};
